#!/bin/sh

# fail if any commands fails
set -e
# debug log
#set -x

show_usage (){
    printf "Usage: $0 [options [parameters]]\n"
    printf "\n"
    printf "Mandatory options:\n"
    printf " -g|--game          [doom|doom2|tnt|plutonia|heretic|hexen]\n"
    printf " -e|--engine        [chocolate|crispy|prboom|dsda|gzdoom]\n"
    printf " -l|--map_limit     [none|vanilla|nolimit|boom|zdoom]\n"
    printf "\n"
    printf "Options:\n"
    printf " -d|--game-dir      [/path/to/doom/base/directory] (Optional, default: '~/games/doom')\n"
    printf " -s|--skill         [1, 2, 3, 4, 5] (Optional, default: '3')\n"
    printf " -r|--map_generator [none|slige|obsidian] (Optional, default: 'none')\n"
    printf " -m|--mods          [none|vanilla|improved|beautiful|brutal|samsara] (Optional, default: 'vanilla')\n"
    printf " -u|--mangohud      [yes|no] (Optional, default: 'no')\n"
    printf " -h|--help, Print help\n"

exit
}

if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
  show_usage
fi
if [ -z "$1" ]; then
  show_usage
fi

while [ -n "$1" ]; do
  case "$1" in
     --game-dir|-d)
         shift
         echo "game directory: $1"
         param_game_dir=$1
         ;;
     --game|-g)
         shift
         echo "game: $1"
         param_iwad=$1
         ;;
     --engine|-e)
         shift
         echo "engine: $1"
         param_engine=$1
         ;;
     --map_limit|-l)
        shift
        echo "map_limit: $1"
        param_map_limit=$1
         ;;
     --skill|-s)
        shift
        echo "skill: $1"
        param_skill=$1
         ;;
     --map_generator|-r)
        shift
        echo "map_generator: $1"
        param_map_generator=$1
         ;;
     --mods|-m)
        shift
        echo "mods: $1"
        param_mods=$1
         ;;
     --mangohud|-u)
        shift
        echo "mangohud: $1"
        param_mangohud=$1
         ;;
     *)
        show_usage
        ;;
  esac
shift
done

### Configuration
if [[ -z $param_game_dir ]]; then
      param_game_dir=~/games/doom
fi
config_iwads_dir=$param_game_dir/wads/iwads
if [ -z "$param_mangohud" ]; then
      param_mangohud=no
fi
if [ -z "$param_map_generator" ]; then
      param_map_generator=none
fi
if [ -z "$param_skill" ]; then
      param_skill=3
fi
if [ -z "$param_mods" ]; then
      param_mods=vanilla
fi


### check parameter values
doom_game="doom doom2"
echo "$doom_game" | tr ' ' '\n' | while read -r item; do
  if [ "$item" = "$param_iwad" ]; then touch match; fi
done
if [ ! -f match ]; then echo "ERROR: $param_iwad is not a valid option, valid options are: $doom_game"; exit 1; fi; rm match

engine="chocolate crispy prboom dsda gzdoom"
echo "$engine" | tr ' ' '\n' | while read -r item; do
  if [ "$item" = "$param_engine" ]; then touch match; fi
done
if [ ! -f match ]; then echo "ERROR: $param_engine is not a valid option, valid options are: $engine"; exit 1; fi; rm match

map_limit="none vanilla nolimit boom zdoom"
echo "$map_limit" | tr ' ' '\n' | while read -r item; do
  if [ "$item" = "$param_map_limit" ]; then touch match; fi
done
if [ ! -f match ]; then echo "ERROR: $param_map_limit is not a valid option, valid options are: $map_limit"; exit 1; fi; rm match

map_generator="none slige obsidian"
echo "$map_generator" | tr ' ' '\n' | while read -r item; do
  if [ "$item" = "$param_map_generator" ]; then touch match; fi
done
if [ ! -f match ]; then echo "ERROR: $param_map_generator is not a valid option, valid options are: $map_generator"; exit 1; fi; rm match

mods="none vanilla improved beautiful brutal samsara"
echo "$mods" | tr ' ' '\n' | while read -r item; do
  if [ "$item" = "$param_mods" ]; then touch match; fi
done
if [ ! -f match ]; then echo "ERROR: $param_mods is not a valid option, valid options are: $mods"; exit 1; fi; rm match

mangohud_enabled="yes no"
echo "$mangohud_enabled" | tr ' ' '\n' | while read -r item; do
  if [ "$item" = "$param_mangohud" ]; then touch match; fi
done
if [ ! -f match ]; then echo "ERROR: $param_mangohud is not a valid option, valid options are: $mangohud_enabled"; exit 1; fi; rm match

skill="1 2 3 4 5"
echo "$skill" | tr ' ' '\n' | while read -r item; do
  if [ "$item" = "$param_skill" ]; then touch match; fi
done
if [ ! -f match ]; then echo "ERROR: $param_skill is not a valid option, valid options are: $skill"; exit 1; fi; rm match


### Script
get_map_file() {
  exclude_files="! -name *tex*.* ! -name *res*.* ! -name *fix.* ! -name *demo*.* ! -name *credits*.*"
  if [[ $param_map_generator == "none" ]]; then
      if [[ $param_map_limit == "none" ]]; then
          if [[ $param_engine == "chocolate" ]]; then
              pwadfile=$(find $param_game_dir/wads/$param_iwad/vanilla/*/*.wad ${exclude_files} -type f 2>/dev/null | shuf -n 1)
          elif [[ $param_engine == "crispy" ]]; then
              pwadfile=$(find $param_game_dir/wads/$param_iwad/{vanilla,nolimit}/*/*.wad ${exclude_files} -type f 2>/dev/null | shuf -n 1)
          elif [[ $param_engine == "prboom" ]]; then
              pwadfile=$(find $param_game_dir/wads/$param_iwad/{vanilla,nolimit,boom}/*/*.wad ${exclude_files} -type f 2>/dev/null | shuf -n 1)
          elif [[ $param_engine == "gzdoom" ]]; then
              pwadfile=$(find $param_game_dir/wads/$param_iwad/{vanilla,nolimit,boom,zdoom}/*/{*.wad,*.pk3} ${exclude_files} -type f 2>/dev/null | shuf -n 1)
          fi
      elif [[ $param_map_limit == "vanilla" ]]; then
          pwadfile=$(find $param_game_dir/wads/$param_iwad/vanilla/*/*.wad ${exclude_files} -type f 2>/dev/null | shuf -n 1)
      elif [[ $param_map_limit == "nolimit" ]]; then
          pwadfile=$(find $param_game_dir/wads/$param_iwad/nolimit/*/*.wad ${exclude_files} -type f 2>/dev/null | shuf -n 1)
      elif [[ $param_map_limit == "boom" ]]; then
          pwadfile=$(find $param_game_dir/wads/$param_iwad/boom/*/*.wad ${exclude_files} -type f 2>/dev/null | shuf -n 1)
      elif [[ $param_map_limit == "zdoom" ]]; then
          pwadfile=$(find $param_game_dir/wads/$param_iwad/zdoom/*/{*.wad,*.pk3} ${exclude_files} -type f 2>/dev/null | shuf -n 1)
      fi
  elif [[ $param_map_generator == "slige" ]]; then
      if [[ $param_map_limit == "vanilla" ]]; then
          echo "Creating new slige map..."
          pwadfile="$param_game_dir/wads/slige/slige_doom2.wad"
          slige_opts="-config $param_game_dir/tools/slige/slige490/slige.cfg -doom2 -levels 1 -rooms 18 -map1 -bimo -biwe -minlight 180 -nocustom"
          $param_game_dir/tools/slige/slige490/slige ${slige_opts} $param_game_dir/wads/slige/slige_doom2.out && $param_game_dir/tools/bsp/bsp-5.2/bsp $param_game_dir/wads/slige/slige_doom2.out -o $pwadfile
          echo "New slige map created... ${pwadfile}"
      else
          echo "ERROR: Slige can only make vanilla maps!"
          exit 1
      fi
  elif [[ $param_map_generator == "obsidian" ]]; then
      echo "Creating new obsidian map..."
      pwadfile="$param_game_dir/wads/obsidian/obsidian_doom2.wad"
      ~/src/Obsidian/obsidian --install ~/src/Obsidian/ --batch $pwadfile --load $param_game_dir/tools/obsidian/configs/${param_iwad}_${param_map_limit}.txt
      echo "New obsidian map created... ${pwadfile}"
  fi

  # Check pwad found
  if [[ -z $pwadfile ]]; then
      echo "No pwad file found"
      echo "### Random game settings"
      echo "param_iwad              : $param_iwad"
      echo "param_engine            : $param_engine"
      echo "PWAD file         : $pwadfile"
      exit 1
  else
      echo "PWAD file: $pwadfile"
  fi

  # Check maps in file
  if [[ $param_map_generator == "none" ]]; then
      pwadfilename=$(awk -F/ '{print $10}' <<< ${pwadfile})
  elif [[ $param_map_generator == "slige" || $param_map_generator == "obsidian" ]]; then
      pwadfilename=$(awk -F/ '{print $8}' <<< ${pwadfile})
  fi
  pwadfilename=$(basename -- "${pwadfilename%.*}")
  echo "PWAD name: $pwadfilename"

  # Get maps from pwad
  gzdoom -iwad $config_iwads_dir/$param_iwad.wad -file $pwadfile -norun -hashfiles > /dev/null || true

  if [[ $param_iwad == "doom2" ]]; then
      pwadmap=$(cat fileinfo.txt | grep $pwadfilename | grep -e " MAP" -e " maps/" | awk '{print $4}' | sed -e "s/^MAP//" -e 's/,//g' | shuf -n 1)
      if [[ -z $pwadmap ]]; then
          pwadmap=$(cat fileinfo.txt | grep $pwadfilename | grep " maps/" | awk '{print $4}' | sed -e "s/^maps\/map//" -e 's/.wad,//g' | shuf -n 1)
      fi
      echo "PWAD map number: $pwadmap"

      mapnumbercheck=$(echo "$pwadmap" | awk '$0 ~/[^0-9]/ { print "NOT_NUMBER" }')
  elif [[ $param_iwad == "doom" ]]; then
      pwadmap=$(cat fileinfo.txt | grep $pwadfilename | grep -E " E[1-5]M" | awk '{print $4}' | shuf -n 1 | sed -r 's/[EM]+/ /g' | sed -e "s/^0//" -e 's/,//g')
      echo "PWAD map number: $pwadmap"
  elif [[ $param_iwad == "heretic" ]]; then
      pwadmap=$(cat fileinfo.txt | grep $pwadfilename | grep -E " E[1-5]M" | awk '{print $4}' | shuf -n 1 | sed -r 's/[EM]+/ /g' | sed -e "s/^0//" -e 's/,//g')
      echo "PWAD map number: $pwadmap"
  elif [[ $param_iwad == "hexen" ]]; then
      pwadmap=$(cat fileinfo.txt | grep $pwadfilename | grep -E " E[1-5]M" | awk '{print $4}' | shuf -n 1 | sed -r 's/[EM]+/ /g' | sed -e "s/^0//" -e 's/,//g')
      echo "PWAD map number: $pwadmap"
  fi

  # Remove temporary file
  rm fileinfo.txt
}

###
# Doom random map script
###
get_map_file

#while [[ -z $pwadfile || $pwadfile == *"/b_"* || $pwadfile == *"_obj_"* || $pwadfile == *"_brk"* || $pwadfile == *"/m_"* || $pwadfile == *"bmodels"* ]]
while [[ -z $pwadmap || $pwadmap == "INFO" || $mapnumbercheck == "NOT_NUMBER" ]]
do
    echo "Incorrect map \"$pwadmap\" getting another map..."
    unset pwadmap
    get_map_file
done

# Save map info in external file
play_combination="${param_iwad},${pwadfile},${pwadmap}"
play_combination=$(sed 's/ /_/g' <<< "${play_combination}")

# Check played times in external file
#if [[ ! "$param_map_generator" == "slige"* && ! "$param_map_generator" == "oblige" && ! "$param_map_generator" == "obsidian"  ]]; then
#    if [ ! -z $(grep "${play_combination}" ${SCRIPT_DIR}/already_played_maps.txt) ]; then
#        echo "Play combination found in file, updating file"
#        current_times=$(cat ${SCRIPT_DIR}/already_played_maps.txt | grep ${play_combination} | awk -F, '{print $4}')
#        played_times=$(echo "$(($current_times + 1))")
#
#        # Update file
#        sed -i "s|${play_combination},${current_times}|${play_combination},${played_times}|g" ${SCRIPT_DIR}/already_played_maps.txt
#    else
#        echo "Play combination not found in file, adding to file"
#        played_times="1"
#        new_played="${play_combination},${played_times}"
#        echo "${new_played}" >> ${SCRIPT_DIR}/already_played_maps.txt
#    fi
#fi

# You can have separate mods "sets" for the source ports
if [[ $param_engine == "gzdoom" ]]; then
    if [[ "$param_iwad" == *"doom"* || "$param_iwad" == "tnt" || "$param_iwad" == "plutonia" ]]; then
        if [[ $param_mods == "none" ]]; then
            MODS=""
        elif [[ $param_mods == "vanilla" ]]; then
            #MODS="$param_game_dir/mods/vanilla/pk_doom_sfx/pk_doom_sfx_20120224.wad $param_game_dir/mods/vanilla/jovian_palette/JovPal.wad"
            MODS="$param_game_dir/mods/vanilla/pk_doom_sfx/pk_doom_sfx_20120224.wad $param_game_dir/mods/vanilla/dimm_pal/doom-pal.wad $param_game_dir/mods/zdoom/vanilla_essence/vanilla_essence_4_3.pk3"
        elif [[ $param_mods == "improved" ]]; then
            MODS="$param_game_dir/mods/vanilla/doom_sound_bulb/doom_sound_bulb.wad $param_game_dir/mods/vanilla/doom_sound_bulb/sound_bulb_extra_sfx.pk3 $param_game_dir/mods/vanilla/dimm_pal/doom-pal.wad $param_game_dir/mods/zdoom/smoothdoom/smoothdoom.pk3 $param_game_dir/mods/vanilla/vbright/vbright.wad $param_game_dir/mods/vanilla/softfx/softfx.wad"
        elif [[ $param_mods == "beautiful" ]]; then
            MODS="$param_game_dir/mods/vanilla/pk_doom_sfx/pk_doom_sfx_20120224.wad $param_game_dir/mods/vanilla/dimm_pal/doom-pal.wad $param_game_dir/mods/zdoom/beautiful_doom/beautiful_doom_716.pk3"
        elif [[ $param_mods == "brutal" ]]; then
            MODS="$param_game_dir/mods/vanilla/pk_doom_sfx/pk_doom_sfx_20120224.wad $param_game_dir/mods/vanilla/dimm_pal/doom-pal.wad $param_game_dir/mods/zdoom/brutal/brutal_doom/brutalv21.13.0.pk3"
        elif [[ $param_mods == "samsara" ]]; then
            MODS="$param_game_dir/mods/vanilla/dimm_pal/doom-pal.wad $param_game_dir/mods/zdoom/samsara/samsara-reincarnation_v1.0b.pk3 $param_game_dir/mods/zdoom/samsara/samsara-remixer_v1.0b.pk3 $param_game_dir/mods/zdoom/samsara/Samsara-Reincarnation_V1.0_OST.pk3"
        fi
    elif [[ "$param_iwad" == "heretic" ]]; then
        if [[ $param_mods == "none" ]]; then
            MODS=""
        elif [[ $param_mods == "vanilla" ]]; then
            MODS="$param_game_dir/mods/vanilla/dimm_pal/her-pal.wad $param_game_dir/mods/vanilla/dwarss_heretic_hq_sfx_pack/sfx.wad $param_game_dir/mods/zdoom/vanilla_essence/vanilla_essence_4_3.pk3"
        elif [[ $param_mods == "improved" ]]; then
            MODS="$param_game_dir/mods/vanilla/dimm_pal/her-pal.wad $param_game_dir/mods/vanilla/dwarss_heretic_hq_sfx_pack/sfx.wad"
        elif [[ $param_mods == "brutal" ]]; then
            MODS="$param_game_dir/mods/vanilla/dimm_pal/her-pal.wad $param_game_dir/mods/zdoom/brutal/brutal_heretic/Heretic-Shadow_Collection/1_BRUTAL_HERETIC/BrutalHereticRPG_V5.0.pk3"
        elif [[ $param_mods == "samsara" ]]; then
            echo "MOD Not available"
            exit 1
        fi
    elif [[ "$param_iwad" == "hexen" ]]; then
        if [[ $param_mods == "none" ]]; then
            MODS=""
        elif [[ $param_mods == "vanilla" ]]; then
            MODS="$param_game_dir/mods/vanilla/dimm_pal/hex-pal.wad"
        elif [[ $param_mods == "improved" ]]; then
            MODS="$param_game_dir/mods/vanilla/dimm_pal/hex-pal.wad $param_game_dir/mods/zdoom/vanilla_essence/vanilla_essence_4_3.pk3"
        elif [[ $param_mods == "brutal" ]]; then
            MODS="$param_game_dir/mods/vanilla/dimm_pal/hex-pal.wad $param_game_dir/mods/zdoom/brutal/brutal_hexen/Hexen/1_BRUTAL_HEXEN/BrutalHexenRPG_V4.7.pk3"
        elif [[ $param_mods == "samsara" ]]; then
            echo "MOD Not available"
            exit 1
        fi
    fi
elif [[ $param_engine == "chocolate" ]]; then
    if [[ "$param_iwad" == *"doom"* || "$param_iwad" == "tnt" || "$param_iwad" == "plutonia" ]]; then
        if [[ $param_mods == "none" ]]; then
            MODS=""
        elif [[ $param_mods == "vanilla" ]]; then
            MODS="$param_game_dir/mods/vanilla/pk_doom_sfx/pk_doom_sfx_20120224.wad $param_game_dir/mods/vanilla/dimm_pal/doom-pal.wad"
        elif [[ $param_mods == "improved" ]]; then
            MODS="$param_game_dir/mods/vanilla/doom_sound_bulb/doom_sound_bulb_legacy.wad $param_game_dir/mods/vanilla/dimm_pal/doom-pal.wad $param_game_dir/mods/vanilla/vbright/vbright.wad $param_game_dir/mods/vanilla/softfx/softfx.wad $param_game_dir/mods/vanilla/vanilla_doom_smooth_weapons/vsmooth.wad -dehlump"
        elif [[ $param_mods == "samsara" ]]; then
            echo "MOD Not available"
            exit 1
        fi
    elif [[ "$param_iwad" == "heretic" ]]; then
        if [[ $param_mods == "none" ]]; then
            MODS=""
        elif [[ $param_mods == "vanilla" ]]; then
            MODS="$param_game_dir/mods/vanilla/dimm_pal/her-pal.wad $param_game_dir/mods/vanilla/dwarss_heretic_hq_sfx_pack/sfx.wad"
        elif [[ $param_mods == "samsara" ]]; then
            echo "MOD Not available"
            exit 1
        fi
    elif [[ "$param_iwad" == "hexen" ]]; then
        if [[ $param_mods == "none" ]]; then
            MODS=""
        elif [[ $param_mods == "vanilla" ]]; then
            MODS="$param_game_dir/mods/vanilla/dimm_pal/hex-pal.wad"
        elif [[ $param_mods == "samsara" ]]; then
            echo "MOD Not available"
            exit 1
        fi
    fi
elif [[ $param_engine == "crispy" ]]; then
    if [[ "$param_iwad" == *"doom"* || "$param_iwad" == "tnt" || "$param_iwad" == "plutonia" ]]; then
        if [[ $param_mods == "none" ]]; then
            MODS=""
        elif [[ $param_mods == "vanilla" ]]; then
            MODS="$param_game_dir/mods/vanilla/pk_doom_sfx/pk_doom_sfx_20120224.wad $param_game_dir/mods/vanilla/dimm_pal/doom-pal.wad"
        elif [[ $param_mods == "improved" ]]; then
            MODS="$param_game_dir/mods/vanilla/doom_sound_bulb/doom_sound_bulb_legacy.wad $param_game_dir/mods/vanilla/dimm_pal/doom-pal.wad $param_game_dir/mods/vanilla/vbright/vbright.wad $param_game_dir/mods/vanilla/softfx/softfx.wad $param_game_dir/mods/vanilla/vanilla_doom_smooth_weapons/vsmooth.wad"
        elif [[ $param_mods == "samsara" ]]; then
            echo "MOD Not available"
            exit 1
        fi
    elif [[ "$param_iwad" == "heretic" ]]; then
        if [[ $param_mods == "none" ]]; then
            MODS=""
        elif [[ $param_mods == "vanilla" ]]; then
            MODS="$param_game_dir/mods/vanilla/dimm_pal/her-pal.wad $param_game_dir/mods/vanilla/dwarss_heretic_hq_sfx_pack/sfx.wad"
        elif [[ $param_mods == "samsara" ]]; then
            echo "MOD Not available"
            exit 1
        fi
    elif [[ "$param_iwad" == "hexen" ]]; then
        if [[ $param_mods == "none" ]]; then
            MODS=""
        elif [[ $param_mods == "vanilla" ]]; then
            MODS="$param_game_dir/mods/vanilla/dimm_pal/hex-pal.wad"
        elif [[ $param_mods == "samsara" ]]; then
            echo "MOD Not available"
            exit 1
        fi
    fi
elif [[ $param_engine == "prboom" ]]; then
    if [[ "$param_iwad" == *"doom"* || "$param_iwad" == "tnt" || "$param_iwad" == "plutonia" ]]; then
        if [[ $param_mods == "none" ]]; then
            MODS=""
        elif [[ $param_mods == "vanilla" ]]; then
            MODS="$param_game_dir/mods/vanilla/pk_doom_sfx/pk_doom_sfx_20120224.wad $param_game_dir/mods/vanilla/dimm_pal/doom-pal.wad"
        elif [[ $param_mods == "improved" ]]; then
            MODS="$param_game_dir/mods/vanilla/doom_sound_bulb/doom_sound_bulb_legacy.wad $param_game_dir/mods/vanilla/dimm_pal/doom-pal.wad $param_game_dir/mods/vanilla/vbright/vbright.wad $param_game_dir/mods/vanilla/softfx/softfx.wad $param_game_dir/mods/vanilla/vanilla_doom_smooth_weapons/vsmooth.wad"
        elif [[ $param_mods == "samsara" ]]; then
            echo "MOD Not available"
            exit 1
        fi
    elif [[ "$param_iwad" == "heretic" ]]; then
        if [[ $param_mods == "none" ]]; then
            MODS=""
        elif [[ $param_mods == "vanilla" ]]; then
            MODS="$param_game_dir/mods/vanilla/dimm_pal/her-pal.wad"
        elif [[ $param_mods == "samsara" ]]; then
            echo "MOD Not available"
            exit 1
        fi
    elif [[ "$param_iwad" == "hexen" ]]; then
        if [[ $param_mods == "none" ]]; then
            MODS=""
        elif [[ $param_mods == "vanilla" ]]; then
            MODS="$param_game_dir/mods/vanilla/dimm_pal/hex-pal.wad"
        elif [[ $param_mods == "samsara" ]]; then
            echo "MOD Not available"
            exit 1
        fi
    fi
fi

if [[ $param_mangohud == "yes" ]]; then
    export MANGOHUD_DLSYM=1
    #export MANGOHUD_CONFIG=cpu_temp,gpu_temp,core_load,cpu_core_clock,gpu_mem_clock,cpu_power,gpu_power,cpu_mhz,ram,vram,frametime,position=top-left,height=500,font_size=24
    export MANGOHUD_CONFIG=cpu_temp,gpu_temp,cpu_core_clock,gpu_mem_clock,cpu_power,gpu_power,cpu_mhz,ram,vram,frametime,position=top-left,height=500,font_size=18
fi

if [[ $param_engine == "chocolate" ]]; then
    if [[ "$param_iwad" == *"doom"* || "$param_iwad" == "tnt" || "$param_iwad" == "plutonia" ]]; then
        commandline="chocolate-doom -config "$param_game_dir"/config/chocolate/config.ini -fullscreen -iwad $config_iwads_dir/$param_iwad.wad -merge $pwadfile $MODS -savedir $param_game_dir/savegames/$param_iwad/ -skill $param_skill -warp $pwadmap"
    elif [[ "$param_iwad" == "heretic" ]]; then
        commandline="chocolate-heretic -config "$param_game_dir"/config/chocolate/config_heretic.ini -fullscreen -iwad $config_iwads_dir/$param_iwad.wad -merge $pwadfile $MODS -savedir $param_game_dir/savegames/$param_iwad/ -skill $param_skill -warp $pwadmap"
    elif [[ "$param_iwad" == "hexen" ]]; then
        commandline="chocolate-hexen -config "$param_game_dir"/config/chocolate/config_hexen.ini -fullscreen -iwad $config_iwads_dir/$param_iwad.wad -merge $pwadfile $MODS -savedir $param_game_dir/savegames/$param_iwad/ -skill $param_skill -warp $pwadmap"
    fi
elif [[ $param_engine == "crispy" ]]; then
    if [[ "$param_iwad" == *"doom"* || "$param_iwad" == "tnt" || "$param_iwad" == "plutonia" ]]; then
        commandline="crispy-doom -config "$param_game_dir"/config/crispy/config_nolimit.ini -fullscreen -iwad $config_iwads_dir/$param_iwad.wad -file $pwadfile $MODS -savedir $param_game_dir/savegames/$param_iwad/ -skill $param_skill -warp $pwadmap"
    elif [[ "$param_iwad" == "heretic" ]]; then
        commandline="crispy-heretic -config "$param_game_dir"/config/crispy/config_nolimit_heretic.ini -fullscreen -iwad $config_iwads_dir/$param_iwad.wad -file $pwadfile $MODS -savedir $param_game_dir/savegames/$param_iwad/ -skill $param_skill -warp $pwadmap"
    elif [[ "$param_iwad" == "hexen" ]]; then
        commandline="crispy-hexen -config "$param_game_dir"/config/crispy/config_nolimit_hexen.ini -fullscreen -iwad $config_iwads_dir/$param_iwad.wad -file $pwadfile $MODS -savedir $param_game_dir/savegames/$param_iwad/ -skill $param_skill -warp $pwadmap"
    fi
elif [[ $param_engine == "prboom" ]]; then
    #commandline="prboom-plus -vidmode gl -complevel 17 -width 1920 -height 1080 -fullscreen -geom 640x360f -aspect 16:9 -iwad $config_iwads_dir/$param_iwad.wad -file $pwadfile $MODS -save $param_game_dir/savegames/$param_iwad/ -skill $param_skill -warp $pwadmap"
    commandline="prboom-plus -config "$param_game_dir"/config/prboom-plus/prboom-plus_boom.cfg -vidmode gl -complevel 17 -width 1920 -height 1080 -fullscreen -aspect 16:9 -iwad $config_iwads_dir/$param_iwad.wad -file $pwadfile $MODS -save $param_game_dir/savegames/$param_iwad/ -skill $param_skill -warp $pwadmap"
elif [[ $param_engine == "dsda" ]]; then
    #commandline="dsda-doom -vidmode gl -complevel 17 -width 1920 -height 1080 -fullscreen -geom 640x360f -aspect 16:9 -iwad $config_iwads_dir/$param_iwad.wad -file $pwadfile $MODS -save $param_game_dir/savegames/$param_iwad/ -skill $param_skill -warp $pwadmap"
    commandline="dsda-doom -config "$param_game_dir"/config/dsda-doom/dsda-doom_boom.cfg  -vidmode gl -complevel 17 -width 1920 -height 1080 -fullscreen -aspect 16:9 -iwad $config_iwads_dir/$param_iwad.wad -file $pwadfile $MODS -save $param_game_dir/savegames/$param_iwad/ -skill $param_skill -warp $pwadmap"
elif [[ $param_engine == "gzdoom" ]]; then
    if [[ "$param_iwad" == *"doom"* || "$param_iwad" == "tnt" || "$param_iwad" == "plutonia" ]]; then
        commandline="gzdoom -config "$param_game_dir"/config/zdoom/config_zdoom_doom.ini -width 1920 -height 1080 -fullscreen -iwad $config_iwads_dir/$param_iwad.wad -file $pwadfile $MODS -savedir $param_game_dir/savegames/$param_iwad/ -skill $param_skill -warp $pwadmap"
    elif [[ "$param_iwad" == "heretic" ]]; then
        commandline="gzdoom -config "$param_game_dir"/config/zdoom/config_zdoom_heretic.ini -width 1920 -height 1080 -fullscreen -iwad $config_iwads_dir/$param_iwad.wad -file $pwadfile $MODS -savedir $param_game_dir/savegames/$param_iwad/ -skill $param_skill -warp $pwadmap"
    elif [[ "$param_iwad" == "hexen" ]]; then
        commandline="gzdoom -config "$param_game_dir"/config/zdoom/config_zdoom_hexen.ini -width 1920 -height 1080 -fullscreen -iwad $config_iwads_dir/$param_iwad.wad -file $pwadfile $MODS -savedir $param_game_dir/savegames/$param_iwad/ -skill $param_skill -warp $pwadmap"
    fi
fi

# Run
if [[ $param_mangohud == "yes" ]]; then
    mangohud $commandline || true
else
    $commandline || true
fi

echo "### Random game settings"
echo "param_iwad              : $param_iwad"
echo "param_engine            : $param_engine"
echo "PWAD file         : $pwadfile"
echo "PWAD map number   : $pwadmap"
echo "MOD files         : $MODS"
echo "param_skill             : $param_skill"
echo "Full command line : $commandline"
echo ""
echo "Iwad/Engine/Map combination: ${play_combination}"
echo "Iwad/Engine/Map combination played ${played_times} times"
